﻿namespace _58最后一个单词的长度
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Solution.LengthOfLastWord("   fly me   to   the moon  "));
        }
    }
    public class Solution
    {
        public static int LengthOfLastWord(string s)
        {
            string[] words = s.Split(' ');
            for (int i = words.Length - 1; i >= 0; i--)
            {
                if (words[i] != "")
                {
                    return words[i].Length;
                }
            }
            return 0;
          
        }
        public static int LengthOfLastWord2(string s)
        {
            //从后向前遍历
            int index = s.Length - 1;
            while (index >= 0 && s[index] == ' ')
            {
                index--;
            }
            int count = 0;
            while (index >= 0 && s[index] != ' ')
            {
                count++;
                index--;
            }
            return count;

        }
    }
}
